﻿using System;
using System.Collections.Generic;
using System.Text;
using Tao.Sdl;

namespace Ommf
{
    public class MouseManager
    {
        private OmmfEngine engine;
        private int x = 0;
        private int y = 0;
        private bool visible = true;
        private byte buttons = 0;

        internal MouseManager(OmmfEngine engine)
        {
            this.engine = engine;
        }

        internal void Update()
        {
            buttons = Sdl.SDL_GetMouseState(out x, out y);
        }

        public bool IsDown(Mouse button)
        {
            switch (button)
            {
                case Mouse.Left: return (buttons & Sdl.SDL_BUTTON(Sdl.SDL_BUTTON_LEFT)) != 0;
                case Mouse.Middle: return (buttons & Sdl.SDL_BUTTON(Sdl.SDL_BUTTON_MIDDLE)) != 0;
                case Mouse.Right: return (buttons & Sdl.SDL_BUTTON(Sdl.SDL_BUTTON_RIGHT)) != 0;
                case Mouse.WheelUp: return (buttons & Sdl.SDL_BUTTON(Sdl.SDL_BUTTON_WHEELUP)) != 0;
                case Mouse.WheelDown: return (buttons & Sdl.SDL_BUTTON(Sdl.SDL_BUTTON_WHEELDOWN)) != 0;
                default: return false;
            }
        }

        public event EventHandler<MouseEventArgs> ButtonPressed;
        internal void OnButtonPressed(Mouse button)
        {
            if (ButtonPressed != null) ButtonPressed(this, new MouseEventArgs(button));
        }

        public event EventHandler<MouseEventArgs> ButtonReleased;
        internal void OnButtonReleased(Mouse button)
        {
            if (ButtonReleased != null) ButtonReleased(this, new MouseEventArgs(button));
        }

        public int X
        {
            get { return x; }
        }

        public int Y
        {
            get { return y; }
        }

        public bool Visible
        {
            get { return visible; }
            set { visible = value; Sdl.SDL_ShowCursor(value ? 1 : 0); }
        }
    }
}
